package org.scboy.common.service.imp;

import org.apache.dubbo.config.annotation.DubboReference;
import org.scboy.common.cache.DynamicCache;
import org.scboy.common.dynamic.DynamicScript;
import org.scboy.common.service.DynamicService;
import org.scboy.coobi.sql.Coobi;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.api.IDynamicManager;
import org.scboy.utils.caches.dbms.entity.DesignEntity;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.client.RestTemplate;

import cn.hutool.core.exceptions.ExceptionUtil;

@Service
public class DynamicServiceImpl implements DynamicService {

	@Autowired
	EntityFactory factory;
	
	@Autowired
	Coobi coobi;
	
	@Autowired
	RestTemplate template;
	
	@DubboReference
	IDynamicManager manager;
	
	@Autowired
	DynamicCache cache;
	

	@Value("${spring.application.name}")
	private String appName="";
	
	@Override
	public Object dynamicServiceExe(String code, Model params){
		DesignEntity design=cache.getService(code, appName);
		if(design==null) {
			throw new RuntimeException("服务不存在。");
		}
		if(design.getActivity()==0) {
			throw new RuntimeException("服务不存在。");
		}
		DynamicScript script=DynamicScript.create(design.getDesignContext(), factory, coobi, template, false);
		return script.run(params);
	}

	@Override
	@Transactional
	public String dynamicServiceExeTest(String code, Model params){
		DesignEntity design=manager.getServiceDesignTest(code, appName);
		DynamicScript script=null;
		try {
			script=DynamicScript.create(design.getDesignContext(), factory, coobi, template, true);
			script.run(params);
			String msg=script.getTestMessage();
			msg+="\r\n测试完成，执行成功,并回滚数据。";
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return msg;
		}catch (Exception e) {
			String message="";
			if(script!=null) {
				message=script.getTestMessage();
			}
			message+="\r\n";
			message+=ExceptionUtil.stacktraceToString(e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return message;
		}
	}

	@Override
	public Object dynamicRequestExe(String code, Model params) {
		DesignEntity design=cache.getRequest(code, appName);
		if(design==null) {
			throw new RuntimeException("404，找不到请求地址。");
		}
		if(design.getActivity()==0) {
			throw new RuntimeException("404，找不到请求地址。");
		}
		DynamicScript script=DynamicScript.create(design.getDesignContext(), factory, coobi, template, false);
		return script.run(params);
	}

	@Override
	@Transactional
	public String dynamicRequestExeTest(String code, Model params){
		DesignEntity design=manager.getRequestDesignTest(code, appName);
		DynamicScript script=null;
		try {
			script=DynamicScript.create(design.getDesignContext(), factory, coobi, template, true);
			script.run(params);
			String msg=script.getTestMessage();
			msg+="\r\n测试完成，执行成功,并回滚数据。";
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return msg;
		}catch (Exception e) {
			String message="";
			if(script!=null) {
				message=script.getTestMessage();
			}
			message+="\r\n";
			message+=ExceptionUtil.stacktraceToString(e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return message;
		}
	}
}
